{% extends "base_jrcadmin2.html" %}

{% load jrc_shared %}

{% block mainheading %}{{ block.super }} \ Signals list \ {{ cst.name }}{% endblock %}

{% block object-tools %}

    <ul class="object-tools">
        <li><a href="/portadmin/">Back to CST</a></li>
        <li><a href="/jrcsignal/{{ cst.pk }}/parse/clean/">Import list (truncating)</a></li>
        <li><a href="/jrcsignal/{{ cst.pk }}/parse/">Append to list</a></li>
    </ul>

{% endblock %}

{% block content %}

    <script type="text/javascript">
        // Store data for every month chart        
        var gData = new Array();
    </script>

    <table>
        <tr>
            <td colspan="2">
                <h3>{{ cst.name }}, {{ cst.market_direction_1.market_id.asset }}, Decimal places: {{ cst.market_direction_1.market_id.price_decimal_places }}</h3>
                <h3>{{ cst.market_direction_name_1 }}
                {% if cst.market_direction_name_2 %}
                    {{ cst.market_direction_name_2 }}
                {% endif %}
                {{ cst.timelevel_m }}m
                </h3>    
            </td>
        </tr>
    {% for y in by_years %}
        {% for m in y.monthes %}
            {# Check if month has signals to display #}
            {#% if not m.is_in_future and m.year >= first_year and m.month >= first_month %#}
            {% if not m.is_in_future and m.is_date_after_first_signal %}
                <tr>
                    <td colspan="2"><strong>{{ m.year }}-{{ m.month }}</strong></td>
                </tr>
                <tr valign="top">
                    <td>
                        <div style="width: 800px; height: {{ m.get_signals_div_height }}px;">
                            
                            <div style="width: 34px;" class="signal_cell_header">PK</div>
                            <div style="width: 129px;" class="signal_cell_header">CST</div>
                            <div class="signal_cell_header">Entry date</div>
                            <div class="signal_cell_header">Exit date</div>
                            <div style="width: 160px;" class="signal_cell_header">&nbsp;</div>
                            <div style="width: 89px;" class="signal_cell_header">Entry value</div>
                            <div style="width: 89px;" class="signal_cell_header">Exit value</div>
                            <div class="signal_cell_header">Profit</div>
                            <div style="width: 39px;" class="signal_cell_header">&nbsp;</div>
                            
                        {% for s in m.get_signals %}
                            <div style="width: 35px;" class="{% cycle "signal_cell_1" "signal_cell_2" as cell %}">{{ s.pk }}</div>
                            <div style="width: 130px;" class="{{ cell }}">{{ s.cst_id.name }}</div>
                            <div class="{{ cell }}">{{ s.date_first|date:"Y-m-d" }}</div>
                            <div class="{{ cell }}">{{ s.date_second|date:"Y-m-d" }}</div>
                            <div style="width: 160px;" class="{{ cell }}">{{ s.get_direction_first_name }} / {{ s.get_direction_second_name }}</div>
                            <div style="width: 90px;" class="{{ cell }}">{{ s.entry_value }}</div>
                            <div style="width: 90px;" class="{{ cell }}">{{ s.exit_value }}</div>
                            
                            <div class="{{ cell }}">{{ s.get_pips|jrc_intformat }}</div>
                            <div style="width: 40px;" class="{{ cell }}">
                                <a href="/jrcsignal/{{ s.pk }}/edit/" class="changelink">&nbsp;</a>
                                <a href="/jrcsignal/{{ s.pk }}/delete/" class="deletelink">&nbsp;</a>
                            </div>
                            
                        {% empty %}
                            <div style="width: 795px; height: 20px;">
                            No signals for <strong>{{ m.year }}-{{ m.month }}</strong>
                            </div>
                        {% endfor %}
                        </div>
                    <td>
                        <div id="ChartDiv_{{ m.year }}_{{ m.month }}">&nbsp;</div>
                        {% if m.get_signals_qnt > 0 %}
                            <script type="text/javascript">
                                gData["{{ m.year }}_{{ m.month }}"] = new Array();
                                {% for v in m.get_aggregated_pips %}
                                    gData["{{ m.year }}_{{ m.month }}"][{{ forloop.counter0 }}] = {{ v }};
                                {% endfor %}
                            </script>
                        {% endif %}
                    </td>
                </tr>
            {% else %}
            {% endif %}
        {% endfor %}
    {% empty %}
        Empty?!
    {% endfor %}
    </table>


    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">

        google.load('visualization', '1', {packages: ['corechart']});

        function drawAllVisualization()
        {
            var divId = "";

            for(var k in gData)
            {
                var data = new google.visualization.DataTable();
                data.addColumn('string', 'Day');
                data.addColumn('number', 'Profit');
                for( d in gData[k])
                {
                    data.addRow([d, gData[k][d]]);
                }
                
                divId = "ChartDiv_" + k;
                new google.visualization.LineChart(
                        document.getElementById(divId)).draw(
                            data, {
                                curveType:"function"
                                , width:300
                                , height:200
                                , vAxis:{maxValue:10}
                            });
            }

        }
        
        google.setOnLoadCallback(drawAllVisualization);
    </script>

{% endblock %}
